package com.augurit.wf.jbpmex.cmd;

import java.util.HashMap;
import java.util.Map;

import org.jbpm.api.ExecutionService;
import org.jbpm.api.JbpmException;
import org.jbpm.api.cmd.Environment;
import org.jbpm.jpdl.internal.activity.TaskActivity;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.augurit.wf.common.constant.WfConstant;

/**
 * 在多工作项模式下完成任务
 */
public class CompleteTaskOfMultiTasksCmd extends AbstractCommand<Void> {

  private static final long serialVersionUID = 1L;
  
  protected String taskId;

  public CompleteTaskOfMultiTasksCmd(String taskId) {
    this.taskId = taskId;
  }

  public Void execute(Environment environment) throws Exception {
    DbSession dbSession = environment.get(DbSession.class);
    
    if (taskId == null || "".equals(taskId)) {
      throw new JbpmException("Cannot complete a task with a null or empty taskId");
    }
    
    TaskImpl task = dbSession.get(TaskImpl.class, Long.parseLong(taskId));
    
    if (task == null) {
      throw new JbpmException("No task with id " + taskId + " was found");
    }
    
    Map<String, Object> params = new HashMap();
    params.put(TaskActivity.CUR_TASK, task);

    task.complete(params, dbSession);
    
    return null;
  }
}
